home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Muddweller 1.2 / source code / Externals / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-26  |  3.4 KB  |  167 lines  |  [TEXT/MPS ]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5. */
  6.  
  7. #include <OSUtils.h>
  8. #include <Errors.h>
  9. #include <Files.h>
  10. #include <Resources.h>
  11. #include <Memory.h>
  12.  
  13. #define OPENRESOLVER    1
  14. #define CLOSERESOLVER    2
  15. #define STRTOADDR        3
  16. #define    ADDRTOSTR        4
  17. #define    ENUMCACHE        5
  18. #define ADDRTONAME        6
  19.  
  20. Handle codeHndl = nil;
  21.  
  22. typedef OSErr (*OSErrProcPtr)();
  23. OSErrProcPtr dnr = nil;
  24.  
  25.  
  26. /* OpenOurRF is called to open the MacTCP driver resources */
  27.  
  28. short OpenOurRF()
  29. {
  30.     SysEnvRec info;
  31.     HParamBlockRec fi;
  32.     Str255 filename;
  33.  
  34.     SysEnvirons(1, &info);
  35.  
  36.     fi.fileParam.ioCompletion = nil;
  37.     fi.fileParam.ioNamePtr = &filename;
  38.     fi.fileParam.ioVRefNum = info.sysVRefNum;
  39.     fi.fileParam.ioDirID = 0;
  40.     fi.fileParam.ioFDirIndex = 1;
  41.     
  42.     while (PBHGetFInfo(&fi, false) == noErr) {
  43.         /* scan system folder for driver resource files of specific type & creator */
  44.         if (fi.fileParam.ioFlFndrInfo.fdType == 'cdev' &&
  45.             fi.fileParam.ioFlFndrInfo.fdCreator == 'mtcp') {
  46.             /* found the MacTCP driver file */
  47.             return(OpenRFPerm(&filename, info.sysVRefNum, fsRdPerm));
  48.             }
  49.         /* check next file in system folder */
  50.         fi.fileParam.ioFDirIndex++;
  51.         fi.fileParam.ioDirID = 0;
  52.         }
  53.     return(-1);
  54.     }    
  55.  
  56.  
  57. OSErr OpenResolver(fileName)
  58. char *fileName;
  59. {
  60.     short refnum;
  61.     OSErr rc;
  62.     
  63.     if (dnr != nil)
  64.         /* resolver already loaded in */
  65.         return(noErr);
  66.         
  67.     /* open the MacTCP driver to get DNR resources. Search for it based on
  68.        creator & type rather than simply file name */    
  69.     refnum = OpenOurRF();
  70.  
  71.     /* ignore failures since the resource may have been installed in the 
  72.        System file if running on a Mac 512Ke */
  73.        
  74.     /* load in the DNR resource package */
  75.     codeHndl = GetIndResource('dnrp', 1);
  76.     if (codeHndl == nil) {
  77.         /* can't open DNR */
  78.         return(ResError());
  79.         }
  80.     
  81.     DetachResource(codeHndl);
  82.     if (refnum != -1) {
  83.         CloseResFile(refnum);
  84.         }
  85.         
  86.     /* lock the DNR resource since it cannot be reloated while opened */
  87.     HLock(codeHndl);
  88.     dnr = (OSErrProcPtr) *codeHndl;
  89.     
  90.     /* call open resolver */
  91.     rc = (*dnr)(OPENRESOLVER, fileName);
  92.     if (rc != noErr) {
  93.         /* problem with open resolver, flush it */
  94.         HUnlock(codeHndl);
  95.         DisposHandle(codeHndl);
  96.         dnr = nil;
  97.         }
  98.     return(rc);
  99.     }
  100.  
  101.  
  102. OSErr CloseResolver()
  103. {
  104.     if (dnr == nil)
  105.         /* resolver not loaded error */
  106.         return(notOpenErr);
  107.         
  108.     /* call close resolver */
  109.     (void) (*dnr)(CLOSERESOLVER);
  110.  
  111.     /* release the DNR resource package */
  112.     HUnlock(codeHndl);
  113.     DisposHandle(codeHndl);
  114.     dnr = nil;
  115.     return(noErr);
  116.     }
  117.  
  118. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  119. char *hostName;
  120. struct hostInfo *rtnStruct;
  121. long resultproc;
  122. char *userDataPtr;
  123. {
  124.     if (dnr == nil)
  125.         /* resolver not loaded error */
  126.         return(notOpenErr);
  127.         
  128.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  129.     }
  130.     
  131. OSErr AddrToStr(addr, addrStr)
  132. unsigned long addr;
  133. char *addrStr;                                    
  134. {
  135.     if (dnr == nil)
  136.         /* resolver not loaded error */
  137.         return(notOpenErr);
  138.         
  139.     (*dnr)(ADDRTOSTR, addr, addrStr);
  140.     return(noErr);
  141.     }
  142.     
  143. OSErr EnumCache(resultproc, userDataPtr)
  144. long resultproc;
  145. char *userDataPtr;
  146. {
  147.     if (dnr == nil)
  148.         /* resolver not loaded error */
  149.         return(notOpenErr);
  150.         
  151.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  152.     }
  153.     
  154.     
  155. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  156. unsigned long addr;
  157. struct hostInfo *rtnStruct;
  158. long resultproc;
  159. char *userDataPtr;                                    
  160. {
  161.     if (dnr == nil)
  162.         /* resolver not loaded error */
  163.         return(notOpenErr);
  164.         
  165.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  166.     }
  167.